import tkinter as tk
from native.common.tk_app import TkApp
from native.excel_file_selector import ExcelFileSelector 
from native.reading_excel import OrderExcelReader
from backend.services.buy_order import BuyOrderService

class MyApp(TkApp):
    def __init__(self, title=""):
        super().__init__(title)

        self.on_load()

    def on_load(self):
        width=570
        height=480
        screenwidth = self._root.winfo_screenwidth()
        screenheight = self._root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self._root.geometry(alignstr)
        self._root.resizable(width=False, height=False)

        self.__file_selector = ExcelFileSelector(self._root, "Excel File:", 10, 10, 450, 30)
        
        self.__btn_import = tk.Button(self._root)
        self.__btn_import["text"] = "Import"
        self.__btn_import.place(x=470, y=10, width=80, height=30)
        self.__btn_import["command"] = self.__on_import

    def __on_import(self):
        print("Import a file:[{}]".format(self.__file_selector.text()));
        buy_orders = OrderExcelReader(self.__file_selector.text()).read_all()
        svr = BuyOrderService()
        for item in buy_orders:
            #print(str(item))
            svr.create(item)



if __name__ == "__main__":
    MyApp(title="Hello world").run()
